package org.ling.xuan.action;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.PsiManager;
import com.intellij.util.IncorrectOperationException;
import org.ling.xuan.condition.LxCondition;
import org.ling.xuan.customnew.HundSunFileTemplate;
import org.ling.xuan.customnew.HundSunFileTemplateDialog;
import org.ling.xuan.dao.LocalAddDao;
import org.ling.xuan.dialog.AddInfoDialog;
import org.ling.xuan.entity.mo.LocalAdd;
import org.ling.xuan.util.LoggerUtil;

public class LxNewAction extends AnAction {
    @Override
    public void actionPerformed(AnActionEvent e) {
        DataContext dataContext = e.getDataContext();
        VirtualFile virtualFile = dataContext.getData(CommonDataKeys.VIRTUAL_FILE);
        String filePath = "";
        // 检查当前文件是否为有效文件
        if (virtualFile != null && virtualFile.isValid()) {
            // 获取当前文件的路径
            filePath = virtualFile.getPath();
        }
        if (StrUtil.isNotBlank(filePath) && FileUtil.isDirectory(filePath)) {
            LocalAdd localAdd = null;
            localAdd = LocalAddDao.getCur();
            if (StrUtil.isBlank(localAdd.getModifyId())) {
                AddInfoDialog addInfoDialog = new AddInfoDialog(e.getProject(), true, localAdd);
                if (addInfoDialog.showAndGet()) {
                    localAdd = addInfoDialog.getSetting();
                    if (StrUtil.isBlank(localAdd.getModifyId()) || StrUtil.isBlank(localAdd.getAddMsg())) {
                        LoggerUtil.log(e.getProject(), "未设置修改信息,无法生成");
                        Messages.showMessageDialog("未设置修改信息,无法生成", "提示", Messages.getInformationIcon());
                        return;
                    } else {
                        LocalAddDao.updateCurAddSimple(localAdd);
                    }
                }
            }

            PsiDirectory directory = PsiManager.getInstance(e.getProject()).findDirectory(virtualFile);
            if (directory != null) {
                HundSunFileTemplateDialog fileTemplateDialog = new HundSunFileTemplateDialog(e.getProject(), true);
                if (fileTemplateDialog.showAndGet()) {
                    HundSunFileTemplate template = fileTemplateDialog.getSelectedTemplate();
                    if (StrUtil.isNotBlank(template.getFileName())) {
                        String targetFileName = template.getFileName() + "." + template.getSuffix();
                        String targetFile = filePath + "/" + targetFileName;
                        boolean create = false;
                        if (FileUtil.exist(targetFile)) {
                            int rs = Messages.showOkCancelDialog("File is exists , replace it ?", "Notice", "Yes", "No", Messages.getQuestionIcon());
                            if (rs == Messages.OK) {
                                create = true;
                                PsiFile file = directory.findFile(targetFileName);
                                if (file != null) {
                                    file.delete();
                                }
                            }
                        } else {
                            create = true;
                        }
                        if (create) {
                            try {
                                PsiFile file = PsiFileFactory.getInstance(e.getProject()).createFileFromText(template.getFileName() + "." + template.getSuffix(), template.getLanguage(), template.getContent());
                                directory.add(file);
                            } catch (IncorrectOperationException ex) {
                                Messages.showErrorDialog("Cannot create file: " + ex.getMessage(), "Error");
                            }
                        }
                    } else {
                        Messages.showErrorDialog("File is exists,Please delete it", "Error");
                    }
                }
            }
        }
    }


    @Override
    public void update(AnActionEvent e) {
        boolean enable = LxCondition.getInstance().isDirectory(e);
        e.getPresentation().setEnabledAndVisible(enable);
    }
}
